package com.service.oos;

import java.io.IOException;
import java.util.*;

import javax.servlet.http.HttpServletResponse;

import com.common.spring.SpringMvcUtils;
import com.dao.oos.CorrectiveActionDao;
import com.model.oos.CorrectiveActionModel;
import com.model.sys.SysUser;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.oos.CorrectiveSuperviseDao;
import com.model.oos.CorrectiveSuperviseModel;
import com.common.model.Global;
import org.springframework.util.CollectionUtils;

@Service
public class CorrectiveSuperviseService {
    static final Logger logger = LoggerFactory.getLogger(CorrectiveSuperviseService.class);

    @Autowired
    CorrectiveSuperviseDao correctiveSuperviseDao;
    @Autowired
    CorrectiveActionDao correctiveActionDao;

    /**
     * 列表查询
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectList(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        SysUser sysUser = SpringMvcUtils.getSysUser();
        // 管理员可以查看全部的数据
        if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
            // 只查询当前用户相关数据
            queryMap.put("dutyPersonCode", sysUser.getLoginname());
        }
        List<CorrectiveSuperviseModel> list = correctiveSuperviseDao.selectList(queryMap);
        int count = correctiveSuperviseDao.selectListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectList:{}", list);
        return httpResult;
    }

    /**
     * 查询本部门行动项列表
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectDepartmentalList(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        SysUser sysUser = SpringMvcUtils.getSysUser();
        // 管理员可以查看全部的数据
        if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
            // 只查询当前用户相关数据
            queryMap.put("dutyPersonCode", sysUser.getLoginname());
        }
        List<CorrectiveSuperviseModel> list = correctiveSuperviseDao.selectDepartmentalList(queryMap);
        int count = correctiveSuperviseDao.selectDepartmentalListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectDepartmentalList:{}", list);
        return httpResult;
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    public HttpResult selectInfor(String supId) {
        HttpResult httpResult = new HttpResult();
        CorrectiveSuperviseModel correctiveSuperviseModel = correctiveSuperviseDao.load(supId);
        httpResult.setData(correctiveSuperviseModel);
        httpResult.setCode(HttpStatus.SUCCESS_CODE);
        return httpResult;
    }

    /**
     * 查询详情
     * 为责任部门提供问题项数据详情查询功能
     *
     * @param corId
     * @return
     */
    public HttpResult selectInforDutyDept(String corId) {
        HttpResult httpResult = new HttpResult();
        CorrectiveActionModel action = correctiveActionDao.load(corId);
        if (null != action) {
            Map params = new HashMap();
            params.put("corId", corId);
            if (null != (this.selectList(params).getRows())) {
                action.setSupervises((List<CorrectiveSuperviseModel>) this.selectList(params).getRows());
            } else {
                action.setSupervises(null);
            }
        }
        httpResult.setData(action);
        httpResult.setCode(HttpStatus.SUCCESS_CODE);
        return httpResult;
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    public HttpResult save(CorrectiveSuperviseModel correctiveSuperviseModel) {
        if (correctiveSuperviseModel == null) {
            return HttpResult.error("参数不能为空");
        }
        HttpResult httpResult = new HttpResult();
        int result = 0;
        if (!StringUtils.isNotNull(correctiveSuperviseModel.getSupId())) {
            correctiveSuperviseModel.setSupId(UUID.randomUUID().toString());
            result = correctiveSuperviseDao.insert(correctiveSuperviseModel);
        } else {
            result = correctiveSuperviseDao.update(correctiveSuperviseModel);
        }
        if (result > 0) {
            httpResult.setMsg(correctiveSuperviseModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(correctiveSuperviseModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(correctiveSuperviseModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 提交
     *
     * @param
     * @return
     */
    public HttpResult commit(CorrectiveSuperviseModel correctiveSuperviseModel) {
        if (correctiveSuperviseModel == null) {
            return HttpResult.error("参数不能为空");
        }
        correctiveSuperviseModel.setStatus(Global.YES + "");
        HttpResult httpResult = new HttpResult();
        int result = 0;
        result = correctiveSuperviseDao.update(correctiveSuperviseModel);
        if (result > 0) {
            httpResult.setMsg(correctiveSuperviseModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(correctiveSuperviseModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(correctiveSuperviseModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    public HttpResult delete(String supId) {
        if (!StringUtils.isNotNull(supId)) {
            return HttpResult.error("参数不能为空");
        }
        CorrectiveSuperviseModel model = correctiveSuperviseDao.load(supId);
        if (!model.getStatus().equals(Global.NO + "")) {
            return HttpResult.error("非待提交状态，不可删除！");
        }
        HttpResult httpResult = new HttpResult();
        int result = correctiveSuperviseDao.delete(supId);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 撤回
     *
     * @param
     * @return
     */
    public HttpResult back(String supId) {
        if (!StringUtils.isNotNull(supId)) {
            return HttpResult.error("参数不能为空");
        }
        CorrectiveSuperviseModel model = correctiveSuperviseDao.load(supId);


        HttpResult httpResult = new HttpResult();
        model.setStatus(Global.NO + "");
        int result = correctiveSuperviseDao.update(model);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 导出
     *
     * @param queryMap
     * @param response
     */
    public void export(Map<String, Object> queryMap, HttpServletResponse response) {
        List<CorrectiveSuperviseModel> list = correctiveSuperviseDao.selectList(queryMap);
        logger.info("export:{}", list);
        try {
            EasyExcel.write(response.getOutputStream(), CorrectiveSuperviseModel.class).sheet("模板").doWrite(list);
        } catch (IOException e) {
            logger.error("导出错误:{}", e.getMessage());
        }

    }
}
